package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.DeviceAdjustRecordMapper;
import com.zhijian.medical.entity.po.DeviceAdjustRecord;
import com.zhijian.medical.entity.vo.request.DeviceAdjustRecordSearchVo;
import com.zhijian.medical.entity.vo.response.DeviceAdjustRecordVo;
import com.zhijian.medical.service.DeviceAdjustRecordService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 校准记录业务处理层
 *
 * @author HCG
 * @date 2022-01-01 15:38:31
 * @version: 1.0
 */
@Slf4j
@Service
public class DeviceAdjustRecordServiceImpl implements DeviceAdjustRecordService {

    @Autowired
    private DeviceAdjustRecordMapper mapper;

    @Override
    public DeviceAdjustRecord selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DeviceAdjustRecordVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(DeviceAdjustRecord deviceAdjustRecord) {
        Date now = new Date();
        deviceAdjustRecord.setCreateTime(now);
        deviceAdjustRecord.setUpdateTime(now);
        return mapper.insert(deviceAdjustRecord) == 1 ? deviceAdjustRecord.getId() : 0L;
    }

    @Override
    public boolean update(DeviceAdjustRecord deviceAdjustRecord) {
        Date now = new Date();
        deviceAdjustRecord.setUpdateTime(now);
        return mapper.updateByPrimaryKey(deviceAdjustRecord) != 0;
    }

    @Override
    public Integer count(DeviceAdjustRecordSearchVo deviceAdjustRecordSearchVo) {
        return mapper.count(deviceAdjustRecordSearchVo);
    }

    @Override
    public List<DeviceAdjustRecordVo> selectList(DeviceAdjustRecordSearchVo deviceAdjustRecordSearchVo) {
        return mapper.selectList(deviceAdjustRecordSearchVo);
    }

    @Override
    public Page<DeviceAdjustRecordVo> selectPage(DeviceAdjustRecordSearchVo deviceAdjustRecordSearchVo) {
        //先直接查总数
        int count = count(deviceAdjustRecordSearchVo);
        //分页
        Page<DeviceAdjustRecordVo> page = new Page<>(true, count, deviceAdjustRecordSearchVo.getPageNum(), deviceAdjustRecordSearchVo.getPageSize());
        deviceAdjustRecordSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DeviceAdjustRecordVo> responseVos = this.selectList(deviceAdjustRecordSearchVo);
        page.setData(responseVos);
        return page;
    }

}